<?php
require_once "model/Dishes.php";

$model = new Dishes;

$day = (int) $_GET['day'];
$day = $day == 0 ? 1 : $day;

$arrID = $arrEat = $arrDrink = $arrSold = $arrDetail = $arrSoldID = $arrEatID = $arrDrinkID = array();

$rs_eat = mysql_query("SELECT * FROM menu WHERE type = 1 AND status = 1 AND day = $day");
while($row_eat = mysql_fetch_assoc($rs_eat)){
	$arrEatID[] = $row_eat['id'];
	$arrID[$row_eat['id']] = $row_eat['id'];
}
$rs_drink = mysql_query("SELECT * FROM menu WHERE type = 2 AND status = 1 AND day = $day");
while($row_drink = mysql_fetch_assoc($rs_drink)){
	$arrDrinkID[] = $row_drink['id'];
	$arrID[$row_drink['id']] = $row_drink['id'];
}
$rs_sold = mysql_query("SELECT * FROM menu WHERE status = 0 AND day = $day");
while($row_sold = mysql_fetch_assoc($rs_sold)){
	$arrSoldID[] = $row_sold['id'];
	$arrID[$row_sold['id']] = $row_sold['id'];
}
if(!empty($arrID)){
	$str_id = implode($arrID,",");

	$sql = "SELECT * FROM dishes WHERE id IN (".$str_id.")";
	$rs = mysql_query($sql) or die(mysql_error());
	while($row_cu = mysql_fetch_assoc($rs)){
		$arrDetail[$row_cu['id']] = $row_cu;
	}
}

//check xem co menu ngay mai hay chưa
$rs_count =mysql_query("SELECT * FROM menu WHERE day = 2 AND status = 1");
$have_tomorrow = mysql_num_rows($rs_count);
?>

<div class="row">

    <div class="col-md-12">    

         <div class="box-header">

                <h3 class="box-title">MENU <?php echo $day == 1 ? "HÔM NAY" : "NGÀY MAI"; ?></h3>

            </div><!-- /.box-header -->
			<p style="text-align:left">
				<button class="btn btn-primary btn-sm" id="btnSave">LƯU MENU</button>
				<?php if($day== 1 && $have_tomorrow > 0){ ?>	
				<button class="btn btn-primary btn-sm right" id="btnUpdate">CẬP NHẬT MENU HÔM NAY</button>
				<?php } ?>
			</p>

			<div class="clear"></div>      
        <div class="box">
            <div class="box-body">
            	<div class="dhe-example-section" id="ex-1-4">

					<div class="dhe-example-section-content">

						<!-- BEGIN: XHTML for example 1.4 -->

						<div id="example-1-4">
							<input type="hidden" name="day" id="day" value="<?php echo $day; ?>"/> 
							<div id="containment">

								<div class="column left first">

									<ul class="sortable-list">
										<?php $rs = mysql_query("SELECT * FROM dishes WHERE status=1 and type = 1"); 
										while($row = mysql_fetch_assoc($rs)){
											if(!in_array($row['id'], $arrID)){
										?>
										<li class="sortable-item" data-value="<?php echo $row['id']; ?>" data-type="<?php echo $row['type']; ?>">
											<img src="../<?php echo $row['image_url'];?>" width="80" /><br />
											<?php echo $row['name']; ?></li>
										<?php } }  ?>										
									</ul>

								</div>
								<div class="column left">

									<ul class="sortable-list">
										<?php $rs = mysql_query("SELECT * FROM dishes WHERE status=1 and type = 2"); 
										while($row = mysql_fetch_assoc($rs)){
											if(!in_array($row['id'], $arrID)){											
										?>
										<li class="sortable-item" data-value="<?php echo $row['id']; ?>" data-type="<?php echo $row['type']; ?>">
											<img src="../<?php echo $row['image_url'];?>" width="80" /><br />
											<?php echo $row['name']; ?></li>
										<?php } } ?>
									</ul>

								</div>
								<div class="column left">
									<p style="background-color:#134a24;text-align:center;color:white;text-transform:uppercase;font-weight:bold;padding:10px">Thức ăn</p>
									<ul class="sortable-list" id="dishes_eat" >
										<?php if(!empty($arrEatID)){ 
											foreach ($arrEatID as $value) {
												$tmp = $arrDetail[$value];
												?>
												<li class="sortable-item" data-value="<?php echo $tmp['id']; ?>" data-type="<?php echo $tmp['type']; ?>">
											<img src="../<?php echo $tmp['image_url'];?>" width="80" /><br />
											<?php echo $tmp['name']; ?></li>
										
												<?php
											}
										?>

										<?php } ?>
									</ul>

								</div>

								<div class="column left">
									<p style="background-color:#134a24;text-align:center;color:white;text-transform:uppercase;font-weight:bold;padding:10px">Nước uống</p>
									<ul class="sortable-list" id="dishes_drink">
										<?php if(!empty($arrDrinkID)){ 
											foreach ($arrDrinkID as $value) {
												$tmp = $arrDetail[$value];
												?>
												<li class="sortable-item" data-value="<?php echo $tmp['id']; ?>" data-type="<?php echo $tmp['type']; ?>">
											<img src="../<?php echo $tmp['image_url'];?>" width="80" /><br />
											<?php echo $tmp['name']; ?></li>
										
												<?php
											}
										?>

										<?php } ?>
									</ul>

								</div>
								<div class="column left">
									<p style="background-color:red;text-align:center;color:white;text-transform:uppercase;font-weight:bold;padding:10px">SOLD OUT</p>
									<ul class="sortable-list" id="dishes_sold">
										<?php if(!empty($arrSoldID)){										
											foreach ($arrSoldID as $value) {
												$tmp = $arrDetail[$value];
												?>
												<li class="sortable-item" data-value="<?php echo $tmp['id']; ?>" data-type="<?php echo $tmp['type']; ?>">
											<img src="../<?php echo $tmp['image_url'];?>" width="80" /><br />
											<?php echo $tmp['name']; ?></li>
										
												<?php
											}
										?>

										<?php } ?>
									</ul>

								</div>
								

								<div class="clearer">&nbsp;</div>

							</div>

						</div>

						<!-- END: XHTML for example 1.4 -->

					</div>
				</div>


			<!-- Example JavaScript files -->
			<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
			<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>

			<!-- Example jQuery code (JavaScript)  -->
			<script type="text/javascript">

			$(document).ready(function(){

				// Example 1.4: Sortable and connectable lists (within containment)
				$('#example-1-4 .sortable-list').sortable({
					connectWith: '#example-1-4 .sortable-list',
					containment: '#containment'
				});
				$("#btnSave").click(function(){
					var countEat = $("#dishes_eat li").length;
					if(countEat > 0){
						var id = type = status = "";
						$("#dishes_eat li").each(function(){
							var value = $(this).attr("data-value");
							id+= value + ",";
							type+= "1,";
							status+= "1,";
						});
						$("#dishes_drink li").each(function(){
							var value = $(this).attr("data-value");
							id+= value + ",";
							type+= "2,";
							status+= "1,";
						});	
						$("#dishes_sold li").each(function(){
							var value = $(this).attr("data-value");
							var t = $(this).attr("data-type");
							id+= value + ",";
							type+= t + ",";
							status+= "0,";
						});			
						$.ajax({
			                url: "ajax/process.php",
			                type: "POST",
			                async: true,
			                data: {
			                    'id' : id,
			                    'type' : type,
			                    'day' : $("#day").val(),
			                    'status' : status,
			                    'action' : "save"
			                },
			                success: function(data){                    			                    
			                    alert("Lưu menu thành công!");                    
			                }
			            });


					}else{
						alert("Bạn chưa chọn món ăn nào!");
						return false;
					}
				});
				$("#btnUpdate").click(function(){
					if(confirm("Bạn chắc chắn cập nhật menu ngày mai thành menu hôm nay ?")){
						$.ajax({
			                url: "ajax/process.php",
			                type: "POST",
			                async: true,
			                data: {			                    
			                    'action' : "update"
			                },
			                success: function(data){                    			                    
			                    alert("Cập nhật thành công!");                    
			                    window.location.reload();
			                }
			            });
					}else{
						return false;
					}
				});
			});

			</script>
               
            </div><!-- /.box-body -->


        </div><!-- /.box -->                           

    </div><!-- /.col -->  

</div>

	<style>
		

.left {float: left;}
.right {float: right;}

.clear,.clearer {clear: both;}
.clearer {
	display: block;
	font-size: 0;
	height: 0;
	line-height: 0;
}

#center-wrapper {
	margin: 0 auto;
	width: 920px;
}

.column {
	margin-left: 2%;
	width: 18%;
	height:700px;
	overflow-y: scroll;
}
.column.first {margin-left: 0;}

.sortable-list {
	background-color: #CCC !important;
	list-style: none;
	margin: 0;
	min-height: 60px;
	padding: 10px;
	min-height: 700px;

}
.sortable-item {
	background-color: #FFF;
	border: 1px solid #000;
	cursor: move;
	display: block;
	font-weight: bold;
	margin-bottom: 5px;
	padding: 20px 0;
	text-align: center;
	text-transform: uppercase;
}

#containment {
	background-color: #FFA;
	
}

.placeholder {
	background-color: #BFB;
	border: 1px dashed #666;
	height: 58px;
	margin-bottom: 5px;
}
	</style>

